/**
 * This program is free software but WITHOUT ANY WARRANTY OR CONDITIONS OF ANY KIND.
 *
 * Copyright (C) 2008-2013 www.pretty-tools.com
 */

document.title = chrome.i18n.getMessage("webslideshow_import_rules_title");

function onPageLoad()
{
    yrprxHTMLContentHelper.enumerateElementsByTagName(document, "translate", function(elem) {
        if (elem && elem.innerText && elem.innerText.length > 0)
            elem.innerText = chrome.i18n.getMessage(elem.innerText);
    });
    yrprxHTMLContentHelper.enumerateElementsByTagName(document, "INPUT", function(elem) {
        if (elem && elem.type && elem.type == "button" && elem.value && elem.value.length > 0)
            elem.value = chrome.i18n.getMessage(elem.value);
    });

    window.arguments = [];
    chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
        if (request.arguments) {
            window.arguments = request.arguments;
            onLoad();
        }
        sendResponse({});
    });

    var body = document.body;
    body.onkeydown = onTableKeypress;
    body.oncontextmenu = body.onselectstart = function() {
        return false;
    };

    document.getElementById("rules_treechildren").onclick = onTableClick;

    document.getElementById("button-ok").onclick = function () {
        if (onOK()) yrprxBrowser.closeWindow();
    };
    document.getElementById("button-cancel").onclick = function () {
        yrprxBrowser.closeWindow();
    };
}

function onTableClick()
{
    var target = window.event.srcElement;
    var tr = yrprxHTMLContentHelper.getParentWithTag(target, "TR");
    if (!tr && target.tagName == "TR")
        tr = target;
    if (!tr)
        return;

    selectTreeNode(gRuleTreeView.findRowIndex(tr));
}

function onTableKeypress()
{
    var event = window.event;
    var keyCode = event.keyCode;

    if (keyCode == yrprxKeyboardHelper.VK_UP && !event.ctrlKey)
        selectTreeNode(gRuleTree.currentIndex-1);
    else if (keyCode == yrprxKeyboardHelper.VK_DOWN && !event.ctrlKey)
        selectTreeNode(gRuleTree.currentIndex+1);
    else
        return true;

    return false;
}

function getAcceptButton()
{
    return document.getElementById("button-ok");
}

addEventListener("load", onPageLoad);
